package com.bigknow.appstore.common;

/**
 * 断言工具类
 * Created by chaos on 2015/11/30.
 */
public final class Assert {

    public static void assertNotNull(Object obj) {
        if (obj == null) throw new NullPointerException();
    }

    public static void assertNotNull(Object obj, String info) {
        if (obj == null) throw new NullPointerException(info);
    }

    public static void assertInteger(Object obj) {
        assertInteger(obj, "[" + obj.toString() + "] is not int");
    }


    public static void assertInteger(Object obj, String info) {
        assertNotNull(obj, info);
        if (obj instanceof Integer) return;
        if (obj instanceof String) {
            try {
                Integer.parseInt((String) obj);
            } catch (Exception e) {
                throw new RuntimeException(info != null ? info : "");
            }
        }
    }

    public static void assertNotNullString(String str, String info) {
        if (str == null || "".equals(str)) {
            throw new NullPointerException(info != null ? info : "");
        }
    }

    public static void assertNotNullString(String str) {
        assertNotNullString(str, null);
    }

    public static void assertWSDLUrl(String url, String info) {
        assertNotNullString(url, info);
        if (!url.toLowerCase().endsWith("?wsdl")) throw new NullPointerException(info != null ? info : "");
    }

    public static void assertWSDLUrl(String url) {
        assertWSDLUrl(url, "地址不对或者没有添加[?wsdl]后缀");
    }

}
